home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Modules / cmathmodule.c < prev    next >
C/C++ Source or Header  |  1999-04-25  |  8KB  |  432 lines

  1. /* Complex math module */
  2.  
  3. /* much code borrowed from mathmodule.c */
  4.  
  5. #include "Python.h"
  6.  
  7. #include "mymath.h"
  8.  
  9. #include "protos/cmathmodule.h"
  10.  
  11. #ifdef i860
  12. /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
  13. #undef HUGE_VAL
  14. #endif
  15.  
  16. #ifdef HUGE_VAL
  17. #define CHECK(x) if (errno != 0) ; \
  18.     else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
  19.     else errno = ERANGE
  20. #else
  21. #define CHECK(x) /* Don't know how to check */
  22. #endif
  23.  
  24. #ifndef M_PI
  25. #define M_PI (3.141592653589793239)
  26. #endif
  27.  
  28. /* First, the C functions that do the real work */
  29.  
  30. /* constants */
  31. static Py_complex c_1 = {1., 0.};
  32. static Py_complex c_half = {0.5, 0.};
  33. static Py_complex c_i = {0., 1.};
  34. static Py_complex c_i2 = {0., 0.5};
  35. #if 0
  36. static Py_complex c_mi = {0., -1.};
  37. static Py_complex c_pi2 = {M_PI/2., 0.};
  38. #endif
  39.  
  40. /* forward declarations */
  41. staticforward Py_complex c_log();
  42. staticforward Py_complex c_prodi();
  43. staticforward Py_complex c_sqrt();
  44.  
  45.  
  46. static Py_complex c_acos(x)
  47.     Py_complex x;
  48. {
  49.     return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
  50.             c_sqrt(c_diff(c_1,c_prod(x,x))))))));
  51. }
  52.  
  53. static char c_acos_doc [] =
  54. "acos(x)\n\
  55. \n\
  56. Return the arc cosine of x.";
  57.  
  58.  
  59. static Py_complex c_acosh(x)
  60.     Py_complex x;
  61. {
  62.     return c_log(c_sum(x,c_prod(c_i,
  63.             c_sqrt(c_diff(c_1,c_prod(x,x))))));
  64. }
  65.  
  66. static char c_acosh_doc [] =
  67. "acosh(x)\n\
  68. \n\
  69. Return the hyperbolic arccosine of x.";
  70.  
  71.  
  72. static Py_complex c_asin(x)
  73.     Py_complex x;
  74. {
  75.     return c_neg(c_prodi(c_log(c_sum(c_prod(c_i,x),
  76.             c_sqrt(c_diff(c_1,c_prod(x,x)))))));
  77. }
  78.  
  79. static char c_asin_doc [] =
  80. "asin(x)\n\
  81. \n\
  82. Return the arc sine of x.";
  83.  
  84.  
  85. static Py_complex c_asinh(x)
  86.     Py_complex x;
  87. {
  88.     /* Break up long expression for WATCOM */
  89.     Py_complex z;
  90.     z = c_sum(c_1,c_prod(x,x));
  91.     z = c_diff(c_sqrt(z),x);
  92.     return c_neg(c_log(z));
  93. }
  94.  
  95. static char c_asinh_doc [] =
  96. "asinh(x)\n\
  97. \n\
  98. Return the hyperbolic arc sine of x.";
  99.  
  100.  
  101. static Py_complex c_atan(x)
  102.     Py_complex x;
  103. {
  104.     return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
  105. }
  106.  
  107. static char c_atan_doc [] =
  108. "atan(x)\n\
  109. \n\
  110. Return the arc tangent of x.";
  111.  
  112.  
  113. static Py_complex c_atanh(x)
  114.     Py_complex x;
  115. {
  116.     return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x))));
  117. }
  118.  
  119. static char c_atanh_doc [] =
  120. "atanh(x)\n\
  121. \n\
  122. Return the hyperbolic arc tangent of x.";
  123.  
  124.  
  125. static Py_complex c_cos(x)
  126.     Py_complex x;
  127. {
  128.     Py_complex r;
  129.     r.real = cos(x.real)*cosh(x.imag);
  130.     r.imag = -sin(x.real)*sinh(x.imag);
  131.     return r;
  132. }
  133.  
  134. static char c_cos_doc [] =
  135. "cos(x)\n\
  136. \n\
  137. Return the cosine of x.";
  138.  
  139.  
  140. static Py_complex c_cosh(x)
  141.     Py_complex x;
  142. {
  143.     Py_complex r;
  144.     r.real = cos(x.imag)*cosh(x.real);
  145.     r.imag = sin(x.imag)*sinh(x.real);
  146.     return r;
  147. }
  148.  
  149. static char c_cosh_doc [] =
  150. "cosh(x)\n\
  151. \n\
  152. Return the hyperbolic cosine of x.";
  153.  
  154.  
  155. static Py_complex c_exp(x)
  156.     Py_complex x;
  157. {
  158.     Py_complex r;
  159.     double l = exp(x.real);
  160.     r.real = l*cos(x.imag);
  161.     r.imag = l*sin(x.imag);
  162.     return r;
  163. }
  164.  
  165. static char c_exp_doc [] =
  166. "exp(x)\n\
  167. \n\
  168. Return the exponential value e**x.";
  169.  
  170.  
  171. static Py_complex c_log(x)
  172.     Py_complex x;
  173. {
  174.     Py_complex r;
  175.     double l = hypot(x.real,x.imag);
  176.     r.imag = atan2(x.imag, x.real);
  177.     r.real = log(l);
  178.     return r;
  179. }
  180.  
  181. static char c_log_doc [] =
  182. "log(x)\n\
  183. \n\
  184. Return the natural logarithm of x.";
  185.  
  186.  
  187. static Py_complex c_log10(x)
  188.     Py_complex x;
  189. {
  190.     Py_complex r;
  191.     double l = hypot(x.real,x.imag);
  192.     r.imag = atan2(x.imag, x.real)/log(10.);
  193.     r.real = log10(l);
  194.     return r;
  195. }
  196.  
  197. static char c_log10_doc [] =
  198. "log10(x)\n\
  199. \n\
  200. Return the base-10 logarithm of x.";
  201.  
  202.  
  203. /* internal function not available from Python */
  204. static Py_complex c_prodi(x)
  205.      Py_complex x;
  206. {
  207.     Py_complex r;
  208.     r.real = -x.imag;
  209.     r.imag = x.real;
  210.     return r;
  211. }
  212.  
  213.  
  214. static Py_complex c_sin(x)
  215.     Py_complex x;
  216. {
  217.     Py_complex r;
  218.     r.real = sin(x.real)*cosh(x.imag);
  219.     r.imag = cos(x.real)*sinh(x.imag);
  220.     return r;
  221. }
  222.  
  223. static char c_sin_doc [] =
  224. "sin(x)\n\
  225. \n\
  226. Return the sine of x.";
  227.  
  228.  
  229. static Py_complex c_sinh(x)
  230.     Py_complex x;
  231. {
  232.     Py_complex r;
  233.     r.real = cos(x.imag)*sinh(x.real);
  234.     r.imag = sin(x.imag)*cosh(x.real);
  235.     return r;
  236. }
  237.  
  238. static char c_sinh_doc [] =
  239. "sinh(x)\n\
  240. \n\
  241. Return the hyperbolic sine of x.";
  242.  
  243.  
  244. static Py_complex c_sqrt(x)
  245.     Py_complex x;
  246. {
  247.     Py_complex r;
  248.     double s,d;
  249.     if (x.real == 0. && x.imag == 0.)
  250.         r = x;
  251.     else {
  252.         s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag)));
  253.         d = 0.5*x.imag/s;
  254.         if (x.real > 0.) {
  255.             r.real = s;
  256.             r.imag = d;
  257.         }
  258.         else if (x.imag >= 0.) {
  259.             r.real = d;
  260.             r.imag = s;
  261.         }
  262.         else {
  263.             r.real = -d;
  264.             r.imag = -s;
  265.         }
  266.     }
  267.     return r;
  268. }
  269.  
  270. static char c_sqrt_doc [] =
  271. "sqrt(x)\n\
  272. \n\
  273. Return the square root of x.";
  274.  
  275.  
  276. static Py_complex c_tan(x)
  277.     Py_complex x;
  278. {
  279.     Py_complex r;
  280.     double sr,cr,shi,chi;
  281.     double rs,is,rc,ic;
  282.     double d;
  283.     sr = sin(x.real);
  284.     cr = cos(x.real);
  285.     shi = sinh(x.imag);
  286.     chi = cosh(x.imag);
  287.     rs = sr*chi;
  288.     is = cr*shi;
  289.     rc = cr*chi;
  290.     ic = -sr*shi;
  291.     d = rc*rc + ic*ic;
  292.     r.real = (rs*rc+is*ic)/d;
  293.     r.imag = (is*rc-rs*ic)/d;
  294.     return r;
  295. }
  296.  
  297. static char c_tan_doc [] =
  298. "tan(x)\n\
  299. \n\
  300. Return the tangent of x.";
  301.  
  302.  
  303. static Py_complex c_tanh(x)
  304.     Py_complex x;
  305. {
  306.     Py_complex r;
  307.     double si,ci,shr,chr;
  308.     double rs,is,rc,ic;
  309.     double d;
  310.     si = sin(x.imag);
  311.     ci = cos(x.imag);
  312.     shr = sinh(x.real);
  313.     chr = cosh(x.real);
  314.     rs = ci*shr;
  315.     is = si*chr;
  316.     rc = ci*chr;
  317.     ic = si*shr;
  318.     d = rc*rc + ic*ic;
  319.     r.real = (rs*rc+is*ic)/d;
  320.     r.imag = (is*rc-rs*ic)/d;
  321.     return r;
  322. }
  323.  
  324. static char c_tanh_doc [] =
  325. "tanh(x)\n\
  326. \n\
  327. Return the hyperbolic tangent of x.";
  328.  
  329.  
  330. /* And now the glue to make them available from Python: */
  331.  
  332. static PyObject *
  333. math_error()
  334. {
  335.     if (errno == EDOM)
  336.         PyErr_SetString(PyExc_ValueError, "math domain error");
  337.     else if (errno == ERANGE)
  338.         PyErr_SetString(PyExc_OverflowError, "math range error");
  339.     else    /* Unexpected math error */
  340.         PyErr_SetFromErrno(PyExc_ValueError); 
  341.     return NULL;
  342. }
  343.  
  344. static PyObject *
  345. math_1(args, func)
  346.     PyObject *args;
  347.     Py_complex (*func) Py_FPROTO((Py_complex));
  348. {
  349.     Py_complex x;
  350.     if (!PyArg_ParseTuple(args, "D", &x))
  351.         return NULL;
  352.     errno = 0;
  353.     PyFPE_START_PROTECT("complex function", return 0)
  354.     x = (*func)(x);
  355.     PyFPE_END_PROTECT(x)
  356.     CHECK(x.real);
  357.     CHECK(x.imag);
  358.     if (errno != 0)
  359.         return math_error();
  360.     else
  361.         return PyComplex_FromCComplex(x);
  362. }
  363.  
  364. #ifdef HAVE_PROTOTYPES
  365. #define FUNC1(stubname, func) \
  366.     static PyObject * stubname(PyObject *self, PyObject *args) { \
  367.         return math_1(args, func); \
  368.     }
  369. #else /* !HAVE_PROTOTYPES */
  370. #define FUNC1(stubname, func) \
  371.     static PyObject * stubname(self, args) PyObject *self, *args; { \
  372.         return math_1(args, func); \
  373.     }
  374. #endif
  375.  
  376. FUNC1(cmath_acos, c_acos)
  377. FUNC1(cmath_acosh, c_acosh)
  378. FUNC1(cmath_asin, c_asin)
  379. FUNC1(cmath_asinh, c_asinh)
  380. FUNC1(cmath_atan, c_atan)
  381. FUNC1(cmath_atanh, c_atanh)
  382. FUNC1(cmath_cos, c_cos)
  383. FUNC1(cmath_cosh, c_cosh)
  384. FUNC1(cmath_exp, c_exp)
  385. FUNC1(cmath_log, c_log)
  386. FUNC1(cmath_log10, c_log10)
  387. FUNC1(cmath_sin, c_sin)
  388. FUNC1(cmath_sinh, c_sinh)
  389. FUNC1(cmath_sqrt, c_sqrt)
  390. FUNC1(cmath_tan, c_tan)
  391. FUNC1(cmath_tanh, c_tanh)
  392.  
  393.  
  394. static char module_doc [] =
  395. "This module is always available. It provides access to mathematical\n\
  396. functions for complex numbers.";
  397.  
  398.  
  399. static PyMethodDef cmath_methods[] = {
  400.     {"acos", cmath_acos, 1, c_acos_doc},
  401.     {"acosh", cmath_acosh, 1, c_acosh_doc},
  402.     {"asin", cmath_asin, 1, c_asin_doc},
  403.     {"asinh", cmath_asinh, 1, c_asinh_doc},
  404.     {"atan", cmath_atan, 1, c_atan_doc},
  405.     {"atanh", cmath_atanh, 1, c_atanh_doc},
  406.     {"cos", cmath_cos, 1, c_cos_doc},
  407.     {"cosh", cmath_cosh, 1, c_cosh_doc},
  408.     {"exp", cmath_exp, 1, c_exp_doc},
  409.     {"log", cmath_log, 1, c_log_doc},
  410.     {"log10", cmath_log10, 1, c_log10_doc},
  411.     {"sin", cmath_sin, 1, c_sin_doc},
  412.     {"sinh", cmath_sinh, 1, c_sinh_doc},
  413.     {"sqrt", cmath_sqrt, 1, c_sqrt_doc},
  414.     {"tan", cmath_tan, 1, c_tan_doc},
  415.     {"tanh", cmath_tanh, 1, c_tanh_doc},
  416.     {NULL,        NULL}        /* sentinel */
  417. };
  418.  
  419. DL_EXPORT(void)
  420. initcmath()
  421. {
  422.     PyObject *m, *d, *v;
  423.     
  424.     m = Py_InitModule3("cmath", cmath_methods, module_doc);
  425.     d = PyModule_GetDict(m);
  426.     PyDict_SetItemString(d, "pi",
  427.                  v = PyFloat_FromDouble(atan(1.0) * 4.0));
  428.     Py_DECREF(v);
  429.     PyDict_SetItemString(d, "e", v = PyFloat_FromDouble(exp(1.0)));
  430.     Py_DECREF(v);
  431. }
  432.